package stego.StegoReComposer;

import java.io.File;
import java.io.FileOutputStream;
import stego.Drivers;
import stego.StegoCreator.StegoExtractor;

public class StegoReComposer_BatchCreator
{

    private static final String strMyClassName = "StegoReComposer_BatchCreator";
    private static File flePathTo_Analyzed_CarrierImages = null;

    public StegoReComposer_BatchCreator(File carrierFile, File topFolder_CarrierFile, boolean imageIsEncrypted, File pathToEncryption_JAR, File pathToEncryption_KEY)
    {
        processFile(carrierFile, topFolder_CarrierFile, imageIsEncrypted, pathToEncryption_JAR, pathToEncryption_KEY);
    }

    private boolean processFile(File fleCarrier, File fleTopFolder_Carrier, boolean fileIsEncrypted, File encryption_JAR_Path, File pathToEncryption_KEY_Path)
    {
        StegoExtractor extractor;
        if(fleCarrier == null || !fleCarrier.exists() || !fleCarrier.isFile())
        {
            throw new Exception("Invalid File Received");
        }
        Drivers.sop((new StringBuilder("\n\nFile: ")).append(fleCarrier).append(" received in ").append("StegoReComposer_BatchCreator").append(".  Commencing Batch Creation...\n").toString());
        flePathTo_Analyzed_CarrierImages = Drivers.createAnalyzedFolder(fleCarrier, fleTopFolder_Carrier);
        extractor = new StegoExtractor(fileIsEncrypted, fleCarrier, encryption_JAR_Path, pathToEncryption_KEY_Path);
        if(extractor.completed_SUCCESSFULLY)
        {
            break MISSING_BLOCK_LABEL_119;
        }
        Drivers.sop((new StringBuilder("Errors were noted on file: ")).append(fleCarrier).append(".  This program will not analyze this file.").toString());
        return false;
        try
        {
            createBatchFragment(extractor, fleCarrier, fleTopFolder_Carrier);
            Drivers.sop((new StringBuilder("Moving ")).append(fleCarrier.getCanonicalPath()).append(" to ").append(flePathTo_Analyzed_CarrierImages.getCanonicalPath()).toString());
            Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c MOVE /Y \"")).append(fleCarrier).append("\"").append(" ").append("\"").append(flePathTo_Analyzed_CarrierImages).append("\"").toString());
        }
        catch(Exception e)
        {
            Drivers.eop("processFile", "StegoReComposer_BatchCreator", e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }

    private boolean createBatchFragment(StegoExtractor extractedMessage, File fleCarrierImage, File fleTopFolder_CarrierImage)
    {
        try
        {
            String strTopFolderPath = fleTopFolder_CarrierImage.getCanonicalPath();
            File fleExpectedBatchPath;
            if(!strTopFolderPath.endsWith(System.getProperty("file.separator")))
            {
                fleExpectedBatchPath = new File((new StringBuilder(String.valueOf(strTopFolderPath))).append(System.getProperty("file.separator")).append(extractedMessage.getBatchIdentifier()).toString());
            } else
            {
                fleExpectedBatchPath = new File((new StringBuilder(String.valueOf(strTopFolderPath))).append(extractedMessage.getBatchIdentifier()).toString());
            }
            if(!fleExpectedBatchPath.exists())
            {
                fleExpectedBatchPath.mkdir();
            }
            File fleExtractedMessage = new File((new StringBuilder()).append(fleExpectedBatchPath).append(System.getProperty("file.separator")).append(extractedMessage.getBatchIdentifier()).append("_").append(extractedMessage.getFragmentNumber()).append("_").append(extractedMessage.getTotalNumberOfFragments()).append("_").append(extractedMessage.getFileExtension()).toString());
            FileOutputStream fos = new FileOutputStream(fleExtractedMessage);
            fos.write(extractedMessage.getExtractedMessage().getBytes());
            fos.flush();
            fos.close();
            Drivers.sop((new StringBuilder("\n# # # Extracted Message Successfully written at: ")).append(fleExtractedMessage.getCanonicalPath()).toString());
        }
        catch(Exception e)
        {
            Drivers.eop("createBatchFragment", "StegoReComposer_BatchCreator", e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }

}
